package com.vrv.netmgr.assets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.Pagination;
import com.vrv.common.UrlPrefix;
import com.vrv.common.util.HttpClientToServer;
import com.vrv.common.util.JsonUtil;
import com.vrv.netmgr.R;
import com.vrv.netmgr.assets.detail.DetailAssetsQryActivityDemo;
import com.vrv.netmgr.vo.DeviceInfo;

/**
 * 注册资产查询demo
 * @author zxb
 *
 */
public class AssetsQryActivityDemo extends Activity{
    private static final String TAG = AssetsQryActivityDemo.class.getSimpleName();
    private static final boolean DEBUG = true;
    
	/**分页对象*/
	public Pagination pagination;
    /**基本查询条件*/
    private EditText et_condition;
    /**基本查询条件封装*/
    private Map<String, String> mNormalQueryCondition;
	/**高级查询条件*/
	private Map<String, String> mAdvancedQueryCondition;
   
	/**等待进度条*/
    private ProgressDialog progressDialog;
    /**查询按钮*/
    private ImageView imgbtn_search;
	/**标题*/
    private TextView tv_title;
	/**返回按钮*/
	private ImageButton imgbtn_back;
	/**数据列表*/
	private ListView listView;
	/**列表的页脚视图*/
	private View loadMoreView;
	private Button loadNextButton;
	private Button loadPreButton;
	
	/**适配器*/
	private RegisterAssetsAdapter assetsAdapter;
	/**适配器数据集合*/
	public List<DeviceInfo> mDeviceInfos;
	
	/**url链接*/
	private String mAssetsUrl;
	/**缓存链接地址*/
	private String mTempUrl;
	
	/**查询条件ip键*/
	private static final String KEY_IPADDRES = "ipaddres";
	/**注册类型*/
	private static final String KEY_REGISTERED_TYPE = "registered";
//	/**未注册*/
//	private static final String REGISTERED_TYPE_0 = "0";
	/**已注册*/
	private static final String REGISTERED_TYPE_1 = "1";

	
	/**查询成功*/
	private static final int QUERY = 0x100;
	/**查询失败*/
	private static final int ERROR = 0x101;
	/**没有数据*/
	private static final int NULL = 0x102;
	
	/**查询方式*/
	private static final int TYPE_0 = 0;
	private static final int TYPE_1 = 1;
	private static final int TYPE_2 = 2;
	private int mTempType = TYPE_0;

	/**PopupWindow相关操作控件*/
	private ImageButton moreFuncBtn;
	private PopupWindow pwMyPopWindow;
	private ListView lvPopupList;
	private ArrayAdapter<String> mArrayAdapter;
	
	@Override
	public void onBackPressed() {
		if(DEBUG) Log.v(TAG, "onBackPressed()");
		
		overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
		super.onBackPressed();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if(DEBUG) Log.v(TAG, "onCreate()");
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.assets_list_page);
//		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_title);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tools_title);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		init();
		bindUI();
		bindListener();
	    iniPopupWindow();
		
		autoQuery();
	}

	/**
	 * 开启查询线程
	 */
	public void runQueryThread() {
		if (DEBUG) Log.v(TAG, "runQueryThread()");

		new Thread(new Runnable() {
			@Override
			public void run() {
				if (null == mDeviceInfos) {
					mDeviceInfos = new ArrayList<DeviceInfo>();
				} else {
					mDeviceInfos.clear();
				}

				String response = query(mTempUrl);
				Log.v(TAG, "runQueryThread()...response = " + response);

				DeviceResponse deviceResponse = analysis(response);
				if (null == deviceResponse) {
					handler.sendEmptyMessage(NULL);
					return;
				}
				
				updateTotalRecords(deviceResponse);
				
				mDeviceInfos = deviceResponse.getDeviceInfos();
				Log.v(TAG, "runQueryThread()...mDeviceInfos.size = " + mDeviceInfos.size());

				if (null != mDeviceInfos || mDeviceInfos.size() > 0) {
					handler.sendEmptyMessage(QUERY);
				}else {
					handler.sendEmptyMessage(ERROR);
				}
			}
		}).start();
	}
	
	/**查询结果为空*/
	public void clearUI(){
		if (DEBUG) Log.v(TAG, "clearUI()");
		
		if (null != progressDialog && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		
		if (listView.getFooterViewsCount() > 0) {
			listView.removeFooterView(loadMoreView);
			listView.setVisibility(View.INVISIBLE);
		}
		
		if (null != mDeviceInfos && mDeviceInfos.size() > 0) {
			mDeviceInfos.clear();
			assetsAdapter.notifyDataSetChanged();
		}
	}
	
	/**
	 * 封装查询参数
	 */
	public void wrapQueryCondition(String condition){
		if (DEBUG) Log.v(TAG, "wrapQueryCondition()");
		
		if (DEBUG) Log.v(TAG, "wrapQueryCondition()...mTempType = " + mTempType);
		
		if (mTempType == TYPE_0 || mTempType == TYPE_1) {
			if (null == mNormalQueryCondition) {
				mNormalQueryCondition = new HashMap<String, String>();
			}
			/**一般查询以IP地址*/
			String ip = et_condition.getText().toString();
			mNormalQueryCondition.put(KEY_IPADDRES, ip);
		}else if (mTempType == TYPE_2){
			if (null == mAdvancedQueryCondition) {
				mAdvancedQueryCondition = new HashMap<String, String>();
			}
			if (null == condition || "".equals(condition)) {
				return;
			}
			
		}
	}
	
	/**
	 * 根据查询类型初始化相关内容
	 * 初始化链接参数
	 * 初始化查询条件
	 * 初始化其他参数
	 */
	public void initQueryType(){
		if (DEBUG) Log.v(TAG, "initQueryType()");
		
		/**链接*/
		if(mTempType == TYPE_0){
			mTempUrl = mAssetsUrl;
		}else if (mTempType == TYPE_1) {
			mTempUrl = mAssetsUrl;
		}
		
		/**和类型相关参数*/
		if(mTempType == TYPE_0){
			
		}else if (mTempType == TYPE_1) {
			
		}else if(mTempType == TYPE_2){
			
		}
		
		/**查询条件*/
		if(mTempType == TYPE_0 || mTempType == TYPE_1){
			mAdvancedQueryCondition = null;
		}else if(mTempType == TYPE_2){
			mNormalQueryCondition = null;
		}
		
	}
	
	
	/**
	 * 自动查询操作
	 */
	public void autoQuery(){
		if(DEBUG) Log.v(TAG, "autoQuery()");
		
		mTempType = TYPE_0;
		
		showWaitDialog();
		wrapQueryCondition(null);
		initQueryType();
		initPagingParameter();
		runQueryThread();
	}
	
	/**
	 * 启动查询等待
	 */
	public void showWaitDialog() {
		if(DEBUG)Log.v(TAG, "showWaitDialog()");
		
		if (null != progressDialog && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		progressDialog = ProgressDialog.show(AssetsQryActivityDemo.this, "", "查询中...", true, true);
	}
	
	/** 消息处理 */
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if(DEBUG) Log.v(TAG, "handler.handleMessage()");
			
			switch (msg.what) {
			case QUERY:
				progressDialog.dismiss();
				updatePaging();
				updateList();
				break;
			case ERROR:
				progressDialog.dismiss();
				Toast.makeText(AssetsQryActivityDemo.this, "网络连接失败", Toast.LENGTH_LONG).show();
				break;
			case NULL:
				clearUI();
				Toast.makeText(AssetsQryActivityDemo.this, "无相关数据", Toast.LENGTH_LONG).show();
			}
		}
	};
	
	/**
	 * 获取查询参数,将所有查询条件合并到map
	 * @param advanced 高级查询条件
	 * @return 返回所有条件map集合
	 */
	public Map<String, String> getQueryParameter(){
		if(DEBUG) Log.v(TAG, "getQueryParameter()");
		
		Map<String, String> map = new HashMap<String, String>();
		
		/**分页参数*/
		map.put(Pagination.KEY_PAGE_NUMBER, "" + pagination.pageNumber);
		map.put(Pagination.KEY_PAGE_SIZE, "" + pagination.pageSize);
		
		/**业务参数*/
		if (null != mAdvancedQueryCondition && mAdvancedQueryCondition.size() > 0) {
			/**高级查询*/
			Set<String> keys = mAdvancedQueryCondition.keySet();
			for (Iterator<String> it = keys.iterator(); it.hasNext();) {
				String key = (String) it.next();
				String value = mAdvancedQueryCondition.get(key);
				
				map.put(key, value);
			}
		}else {
			/**一般查询以IP地址*/
			Set<String> keys = mNormalQueryCondition.keySet();
			for (Iterator<String> it = keys.iterator(); it.hasNext();) {
				String key = (String) it.next();
				String value = mNormalQueryCondition.get(key);
				
				map.put(key, value);
			}
		}
		/**已注册机器*/
		map.put(KEY_REGISTERED_TYPE, REGISTERED_TYPE_1);
		
		return map;
	}
	
	/**
	 * 初始化查询参数
	 */
	public void initPagingParameter(){
		if(DEBUG) Log.v(TAG, "initQueryParameter()");
		
		if (null == pagination) {
			pagination = new Pagination();
		}
		
		/**this is test*/
		pagination.pageNumber = 0;
		pagination.totalRecords = 0;
		pagination.pageSize = 20;
	}
	
	/**
	 * 更新记录总数和分页查询相关
	 * @param response 服务器返回的数据结构体
	 */
	public void updateTotalRecords(DeviceResponse response){
		if(DEBUG) Log.v(TAG, "updatePagingParameter()");
		
		if (null == response) {
			return;
		}
		
		pagination.totalRecords = response.getTotalRecords();
	}
	
	/**
	 * 更新查询页号
	 * @param paging翻页标记true表示下一页，false表示上一页
	 */
	public void updatePageNumber(boolean paging){
		if(DEBUG) Log.v(TAG, "updateQueryParameter()");
		
		if (paging) {
			pagination.pageNumber += 1;
		}else {
			pagination.pageNumber -= 1;
		}
	}
	
	/**
	 * 查询数据
	 * @param url 模块链接
	 * @param advanced 高级查询条件
	 * @return 以字符串返回服务器查询结果
	 */
	public String query(String url){
		if(DEBUG) Log.v(TAG, "query()");
		
		String ret = "";
		
		HttpClientToServer httpClientToServer = new HttpClientToServer();
		
		Map<String, String> map = getQueryParameter();
		if(DEBUG) Log.v(TAG, "query()...map = " + map);
		
		String response = httpClientToServer.doPost(url, map);
		if (!HttpClientToServer.DO_POST_ERR.equals(response) && !HttpClientToServer.DO_POST_EXCEPT.equals(response)) {
			ret = response;
		}
		
		return ret;
	}
	
	/**
	 * 更新分页控件显示
	 */
	public void updatePaging(){
		if(DEBUG) Log.v(TAG, "updatePaging()");

		if (listView.getFooterViewsCount() > 0) {
			listView.removeFooterView(loadMoreView);
		}
		
		loadNextButton.setText(R.string.seeNextPage);
		loadPreButton.setText(R.string.seePrePage);
		
		if (pagination.totalRecords <= pagination.pageSize) {
			/**不显示分页控件*/
			loadNextButton.setVisibility(View.GONE);
			loadPreButton.setVisibility(View.GONE);
			
			if(DEBUG) Log.v(TAG, "updatePaging()...不显示分页控件");
		}else if (pagination.totalRecords > pagination.pageSize && 0 == pagination.pageNumber && true) {
			/**显示下一页*/
			loadNextButton.setVisibility(View.VISIBLE);
			loadPreButton.setVisibility(View.GONE);
			
			if(DEBUG) Log.v(TAG, "updatePaging()...显示下一页");
		}else if (pagination.totalRecords > pagination.pageSize && pagination.pageNumber > 0 && ((pagination.pageNumber + 1) * pagination.pageSize < pagination.totalRecords) && true) {
			/**显示上一页、下一页*/
			loadNextButton.setVisibility(View.VISIBLE);
			loadPreButton.setVisibility(View.VISIBLE);
			
			if(DEBUG) Log.v(TAG, "updatePaging()...显示上一页、下一页");
		}else if (pagination.totalRecords > pagination.pageSize && pagination.pageNumber > 0 && ((pagination.pageNumber + 1) * pagination.pageSize >= pagination.totalRecords) && true) {
			/**显示上一页*/
			loadNextButton.setVisibility(View.GONE);
			loadPreButton.setVisibility(View.VISIBLE);
			
			if(DEBUG) Log.v(TAG, "updatePaging()...显示上一页");
		}
		
		listView.addFooterView(loadMoreView);
	}
	
	
	/**
	 * 解析服务器返回的字符串
	 * @param response 服务器返回的字符串
	 * @return 返回指定的数据结构体
	 */
	public DeviceResponse analysis(String response){
		if(DEBUG) Log.v(TAG, "analysis()");
		
		DeviceResponse deviceResponse = new DeviceResponse();
		
		List<DeviceInfo> deviceInfos = null;
		
		if (null == response || "".equals(response)) {
			return null;
		}

		try {
			JSONTokener jsonParser = new JSONTokener(response);
			JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
			
			boolean b = jsonObj.getBoolean("success");
			deviceResponse.setSuccess(b);
			if (!b) {
				return null;
			}
			
			int count = jsonObj.getInt("totalRecords");
			deviceResponse.setTotalRecords(count);
			if (count <= 0) {
				return null;
			}
			
			JSONArray jArr = jsonObj.getJSONArray("records");
			deviceResponse.setRecords(jArr.length());
			if (null == jArr || jArr.length() == 0) {
				return null;
			}
			
			deviceInfos = new ArrayList<DeviceInfo>();
			
			int len = jArr.length();
			for (int i = 0; i < len; i++) {
				JSONObject jObj = (JSONObject) jArr.opt(i);
				
				DeviceInfo deviceInfo = new DeviceInfo();
				
				deviceInfo.setClassName(JsonUtil.getJsonString(jObj, "className"));
				deviceInfo.setDeptName(JsonUtil.getJsonString(jObj, "deptName"));
				deviceInfo.setOfficeName(JsonUtil.getJsonString(jObj, "officeName"));
				deviceInfo.setTel(JsonUtil.getJsonString(jObj, "tel"));
				deviceInfo.setUserName(JsonUtil.getJsonString(jObj, "userName"));
				deviceInfo.setIpaddres(JsonUtil.getJsonString(jObj, "ipaddres"));
				deviceInfo.setMacAddress(JsonUtil.getJsonString(jObj, "macAddress"));
				deviceInfo.setDeviceName(JsonUtil.getJsonString(jObj, "deviceName"));
				deviceInfo.setDescription(JsonUtil.getJsonString(jObj, "description"));
				
				deviceInfos.add(deviceInfo);
			}
			
			deviceResponse.setDeviceInfos(deviceInfos);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return deviceResponse;
	}
	
	/**
	 * 更新UI列表
	 */
	public void updateList(){
		if(DEBUG) Log.v(TAG, "updateList()");
		
		if (null == assetsAdapter) {
			assetsAdapter = new RegisterAssetsAdapter(mDeviceInfos, AssetsQryActivityDemo.this);
		}else {
			assetsAdapter.mDeviceInfos = mDeviceInfos;
		}
		
		listView.setVisibility(View.VISIBLE);
		listView.setAdapter(assetsAdapter);
		
		assetsAdapter.notifyDataSetChanged();
	}
	
	/**
	 * LISTVIEW适配器继承BaseAdapter
	 * @author zxb
	 *
	 */
	class RegisterAssetsAdapter extends BaseAdapter {

		public Context mContext;
		public List<DeviceInfo> mDeviceInfos;

		public RegisterAssetsAdapter(List<DeviceInfo> list, Context context) {
			this.mDeviceInfos = list;
			this.mContext = context;
		}

		@Override
		public int getCount() {
			return mDeviceInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return mDeviceInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null){
				convertView = LayoutInflater.from(AssetsQryActivityDemo.this).inflate(R.layout.assetsqry_listpage_item, null);
			}
			
			TextView depName = (TextView) convertView.findViewById(R.id.tv_depname);
			TextView devIP = (TextView) convertView.findViewById(R.id.tv_devip);
			TextView devName = (TextView) convertView.findViewById(R.id.tv_devname);
			
			depName.setText(mDeviceInfos.get(position).getDeptName());
			devIP.setText(mDeviceInfos.get(position).getIpaddres());
			devName.setText(mDeviceInfos.get(position).getDeviceName());
			
			return convertView;
		}

	}
	
	/**
	 * LISTVIEW列表事件继承OnItemClickListener
	 * @author zxb
	 *
	 */
	private class OnItemClick implements AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			DeviceInfo deviceInfo = mDeviceInfos.get(arg2);
			
			Intent intent = new Intent(AssetsQryActivityDemo.this, DetailAssetsQryActivityDemo.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("info", deviceInfo);
			intent.putExtras(bundle);
			startActivity(intent);
		}
		
	}
	
	/**
	 * 查询按钮事件
	 * @author zxb
	 *
	 */
	private class SearchOnBtnClick implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			if(DEBUG) Log.v(TAG, "SearchOnBtnClick.onClick()");
			
			showWaitDialog();
			wrapQueryCondition(null);
			initQueryType();
			initPagingParameter();
			runQueryThread();
		}
		
	}
	

	/**
	 * 初始化popupwindow
	 */
	private void iniPopupWindow() {
		if(DEBUG) Log.v(TAG, "iniPopupWindow()");
		
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.title_func_popupwindow, null);
		
		/**list选项*/
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		
		pwMyPopWindow = new PopupWindow(layout);
		// 加上这个popupwindow中的ListView才可以接收点击事件
		pwMyPopWindow.setFocusable(true);
        
		String[] arr = getResources().getStringArray(R.array.softwareSearchCondition);
		if (null == itemDatas) {
			itemDatas = new ArrayList();
		} else {
			itemDatas.clear();
		}
		for (int i = 0; i < arr.length; i++) {
			PopItem item = new PopItem();
			item.title = arr[i];
			item.check = false;
			if (0 == i) {
				item.check = true;
			}
			itemDatas.add(item);
		}
		if (null == pAdapter) {
			pAdapter = new PopWindowAdapter(itemDatas, AssetsQryActivityDemo.this);
		}
		lvPopupList.setAdapter(pAdapter);
		lvPopupList.setOnItemClickListener(new PopupWindowOnItemClickListener());
		
		
		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		// pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()) * 3);
		pwMyPopWindow.setHeight(LayoutParams.WRAP_CONTENT);

		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));
		// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
		pwMyPopWindow.setOutsideTouchable(true);
	}
	
	/**
	 * 初始化popupwindow事件
	 */
	private class PopupWindowOnItemClickListener implements AdapterView.OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view,
				final int position, long id) {
			if(DEBUG) Log.v(TAG, "PopupWindowOnItemClickListener.onItemClick()");
			
			if(DEBUG) Log.v(TAG, "PopupWindowOnItemClickListener.onItemClick()...position = " + position) ;
			
			pwMyPopWindow.dismiss();
			/**根据查询方式查询数据*/
			if (0 == position) {
				mTempType = TYPE_0;
				
				showWaitDialog();
				wrapQueryCondition(null);
				initQueryType();
				initPagingParameter();
				runQueryThread();
			}else if (1 == position) {
				mTempType = TYPE_1;
				
				showWaitDialog();
				wrapQueryCondition(null);
				initQueryType();
				initPagingParameter();
				runQueryThread();
			}else if (2 == position) {
				/**高级查询*/
			}
			
//			/**提示用户目前查询方式*/
//			Toast.makeText(AssetsQryActivityDemo.this, mArrayAdapter.getItem(position).toString(), Toast.LENGTH_LONG).show();
		}
	}

    public List<PopItem> itemDatas;
    public PopWindowAdapter pAdapter;
    
    public class PopWindowAdapter extends BaseAdapter{
    	public List<PopItem> lst;
    	public Context mContext;
    	
    	public PopWindowAdapter(List<PopItem> lst, Context context){
    		this.lst = lst;
    		this.mContext = context;
    	}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lst.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return lst.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = LayoutInflater.from(AssetsQryActivityDemo.this).inflate(R.layout.list_item_popupwindow, null);
            }
            CheckBox cBox = (CheckBox) convertView.findViewById(R.id.check);
            TextView title =(TextView) convertView.findViewById(R.id.tv_list_item);
            
            cBox.setChecked(lst.get(position).check);
            title.setText(lst.get(position).title);
			
			return convertView;
		}
    	
    }
    
	public class PopItem {
		public String title;
		public boolean check = false;
	} 
	
	/**
	 *  返回数据解析结果
	 * @author zxb
	 *
	 */
	class DeviceResponse{
		public boolean success;
		public int totalRecords;
		public int records;
		List<DeviceInfo> deviceInfos;
		
		public boolean isSuccess() {
			return success;
		}
		public void setSuccess(boolean success) {
			this.success = success;
		}
		public int getTotalRecords() {
			return totalRecords;
		}
		public void setTotalRecords(int totalRecords) {
			this.totalRecords = totalRecords;
		}
		public int getRecords() {
			return records;
		}
		public void setRecords(int records) {
			this.records = records;
		}
		public List<DeviceInfo> getDeviceInfos() {
			return deviceInfos;
		}
		public void setDeviceInfos(List<DeviceInfo> deviceInfos) {
			this.deviceInfos = deviceInfos;
		}
	}
	
	/**
	 * 数据初始化
	 */
	private void init(){
		if(DEBUG) Log.v(TAG, "init()");
	
		initUrl();
	}
	
	/**
	 * 链接初始化
	 */
	private void initUrl(){
		if(DEBUG) Log.v(TAG, "initUrl()");
		
		UrlPrefix urlPrefix = new UrlPrefix(this);
		String prefix = urlPrefix.getUrlPrefix();
		mAssetsUrl = prefix + "/" + "deviceInfo" + "/" + "jsonList.do";
	}
	
	/**
	 * 初始化UI
	 */
	public void bindUI(){
		if(DEBUG) Log.v(TAG, "bindUI()");
		
		tv_title = (TextView) findViewById(R.id.title_text);
		tv_title.setText("注册资产查询");
		//
		listView = (ListView) findViewById(R.id.assets_lv_id);
		//
		imgbtn_back = (ImageButton) findViewById(R.id.backBtn);
		imgbtn_back.setVisibility(View.VISIBLE);
		//
		et_condition = (EditText)findViewById(R.id.assets_conditionQry);
		imgbtn_search = (ImageView)findViewById(R.id.search_go_btn);
		//
		loadMoreView = getLayoutInflater().inflate(R.layout.list_page_load, null);
		loadNextButton = (Button) loadMoreView.findViewById(R.id.loadNextButton);
		loadPreButton = (Button) loadMoreView.findViewById(R.id.loadPreButton);
		/**更多操作按钮*/
		moreFuncBtn = (ImageButton) findViewById(R.id.moreFunctionBtn);
	}
	
	/**
	 * 绑定监听事件
	 */
	public void bindListener() {
		if(DEBUG) Log.v(TAG, "bindListener()");
		
		imgbtn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		//
		listView.setOnItemClickListener(new OnItemClick());
		//
		imgbtn_search.setOnClickListener(new SearchOnBtnClick());
		//
		loadNextButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadNextButton.setText(R.string.waitingPage);
				
				updatePageNumber(true);
				
				runQueryThread();
			}
		});
		//
		loadPreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadPreButton.setText(R.string.waitingPage);
				
				updatePageNumber(false);
				
				runQueryThread();
			}
		});
		/**更多操作按钮*/
		moreFuncBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (pwMyPopWindow.isShowing()) {
					pwMyPopWindow.dismiss();// 关闭
				} else {
					pwMyPopWindow.showAsDropDown(moreFuncBtn);// 显示
				}
			}
		});
	}

}
